<%-- 
    Document   : products
    Created on : Mar 12, 2013, 5:49:03 PM
    Author     : DungtTT60215
--%>
<%@page import="BLO.ProductsBLO"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<html>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/xmlproject.js"></script>

    <script type="text/javascript">
        //dung      
        var allProductDOM=null;
        var new_XMLDOM=null;
        var txtFrom;
        var txtTo;
        function handleEnterName(e){
            var charCode = (e.which || e.which == 0) ? e.which : event.keyCode //e.which==0 is for firefox
            if (charCode > 32 && (charCode < 48 ||
                (charCode > 57 && charCode < 65) ||
                (charCode > 90 && charCode < 97) || charCode > 122)  )
                return false;
            return true;
        }
        function handleEnterNumber(e){
            var charCode = (e.which || e.which == 0) ? e.which : event.keyCode //e.which==0 is for firefox
            if ((charCode > 31 && charCode < 48 )||charCode > 57 )
                return false;
            return true;
        }
        function validate(from,to){
            
           
            txtFrom=from;
            txtTo=to;

        }
        function getHttpRequest(){
            try{
                //ff
                //xmlDom = new XMLHttpRequest();
                allProductDOM= document.implementation.createDocument("","",null);
            }
            catch(e){
                try{
                    allProductDOM = new ActiveXObject("Msxm12:XMLHTTP");
                }
                catch(e){
                    try{
                        allProductDOM = new ActiveXObject("Microsoft.XMLHTTP")
                    }catch(e){
                        alert(e);
                    }
                }
            }
        }
        function getProducts(filename){
            //            allProductDOM=getAllProducts(filename);
            try{
                getHttpRequest();
                if(allProductDOM!=null){
                    allProductDOM.async=false;
                    allProductDOM.load(filename);
                }
            }
            catch(e){
                alert(e);
            }
        }
        function convertText(doc){
            var xmlContent;
            try{
                if(window.ActiveXObject){
                    xmlContent = doc.documentElement.xml;
                }else{
                    xmlContent = new XMLSerializer().serializeToString(doc);
                }
                return xmlContent;
            }catch(e){
                return "";
                e.toString();
            }
        }
        function combineXMLwithXSL(xml,xsl){
            if (window.ActiveXObject) {
                var out = xml.transformNode(xsl);
                document.getElementById("all-products").innerHTML = out;
            } else {
                var xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(xsl);
                resultDocument = xsltProcessor.transformToFragment(xml, document);
                document.getElementById("all-products").innerHTML= "";
                document.getElementById("all-products").innerHTML=   convertText(resultDocument);
                //                document.getElementById("list-product").appendChild("");
            }
        }
        function SearchProduct(){
            try{
                allProductDOM=returnAllProductsDom();
                if (allProductDOM==null){
                    allProductDOM=getProducts("xml/allProducts.xml");
                }
                if (allProductDOM==null){
                    return;
                }

                var xsl =document.implementation.createDocument("","",null);
                xsl.async=false;
                xsl.load('xsl/allProducts.xsl');

                var name = document.getElementById("txtSearchName").value.trim().replace(/\s{2,}/g, ' ');
                var from = document.getElementById("priceFrom").value.trim().replace(/\s{2,}/g, ' ');
                var to = document.getElementById("priceTo").value.trim().replace(/\s{2,}/g, ' ');
             
                if((name.length==0)&&(from.length==0)&&(to.length==0)){
                    combineXMLwithXSL(allProductDOM,xsl);
                    return;
                }
                if(from.length==0){
                    from=0;
                }
                if(to.length==0){
                    to=99999;
                }
//                if(to<from){
//                    alert("From-Number is larger than To-Number. Please input again!");
//                    return;
//                }
//                validate(from,to);
                var str="//product[productPrice>"+from+ " and productPrice<"+to+
                    " and contains(translate(productName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"+name.toLowerCase()+
                    "')]";
                var result=allProductDOM.evaluate(str, allProductDOM,null, XPathResult.ANY_TYPE , null);

                var param = result.iterateNext();
                if(param==null){
                    document.getElementById("all-products").innerHTML= "";
                    return;
                }
                else{
                    new_XMLDOM = "<products>";
                    while (param) {
                        new_XMLDOM+="<product>";
                        new_XMLDOM+="<productID>"+ param.getElementsByTagName("productID")[0].childNodes[0].nodeValue+"</productID>";
                        new_XMLDOM+="<brandID>"+param.getElementsByTagName("brandID")[0].childNodes[0].nodeValue+"</brandID>";
                        new_XMLDOM+="<productName>"+param.getElementsByTagName("productName")[0].childNodes[0].nodeValue+"</productName>";
                        new_XMLDOM+="<productPrice>"+param.getElementsByTagName("productPrice")[0].childNodes[0].nodeValue+"</productPrice>";
                        new_XMLDOM+="<productImage>"+param.getElementsByTagName("productImage")[0].childNodes[0].nodeValue+"</productImage>";
                        new_XMLDOM+="<shortDecs>"+param.getElementsByTagName("shortDecs")[0].childNodes[0].nodeValue+"</shortDecs>";
                        new_XMLDOM+="<longDecs>"+param.getElementsByTagName("longDecs")[0].childNodes[0].nodeValue+"</longDecs>";
                        new_XMLDOM+="<monitor>"+param.getElementsByTagName("monitor")[0].childNodes[0].nodeValue+"</monitor>";
                        new_XMLDOM+="<cpu>"+param.getElementsByTagName("cpu")[0].childNodes[0].nodeValue+"</cpu>";
                        new_XMLDOM+="<ram>"+param.getElementsByTagName("ram")[0].childNodes[0].nodeValue+"</ram>";
                        new_XMLDOM+="<wifi>"+param.getElementsByTagName("wifi")[0].childNodes[0].nodeValue+"</wifi>";
                        new_XMLDOM+="<os>"+param.getElementsByTagName("os")[0].childNodes[0].nodeValue+"</os>";
                        new_XMLDOM+="<pin>"+param.getElementsByTagName("pin")[0].childNodes[0].nodeValue+"</pin>";
                        new_XMLDOM+="<others>"+param.getElementsByTagName("others")[0].childNodes[0].nodeValue+"</others>";
                        new_XMLDOM+="<sold>"+param.getElementsByTagName("sold")[0].childNodes[0].nodeValue+"</sold>";
                        new_XMLDOM+="<createDate>"+param.getElementsByTagName("createDate")[0].childNodes[0].nodeValue+"</createDate>";
                        new_XMLDOM+="<status>"+param.getElementsByTagName("status")[0].childNodes[0].nodeValue+"</status>";
                        new_XMLDOM+="</product>";
                        param = result.iterateNext();
                    }
                    new_XMLDOM+="</products>";
                    var parser = new DOMParser();
                    new_XMLDOM = parser.parseFromString(new_XMLDOM, "text/xml");
                    new_XMLDOM.async=false;
                    combineXMLwithXSL(new_XMLDOM,xsl);
                }                
            }
            catch(e){alert(e);
                return;
            }
        }
        // mouse event
        
        function productMouserOver(id){
            document.onmousemove=setTooltipPos;
            document.getElementById("Product-Detail").style.display="block";
            var info = document.getElementById('productId'+id).innerHTML;
            document.getElementById("Product-Detail").innerHTML=info;
            //lay vi tri con chuot
           

        }
        //        function A(){document.getElementById("Product-Detail").style.display='none';}
        function productMouseOut(){
            //setInterval(A, 1000);
            //sleep(1);
            document.getElementById("Product-Detail").style.display='none';
            //setTimeout("A()", 400);
        }
        function setTooltipPos(e){
            try{
                if(!e) e = event ;
                var mp = getMousePosition(e);
                document.getElementById("Product-Detail").style.top=(mp.y+5)+'px';
                document.getElementById("Product-Detail").style.left=(mp.x-300)+'px';
            }catch(e){
                alert(e);
            }
        }
        function getMousePosition(e){
            return e.pageX?{'x':e.pageX,'y':e.pageY}:{
                'x':e.clientX+document.documentElement.scrollLeft+document.body.scrollLeft,
                'y':e.clientY+document.documentElement.scrollTop+document.body.scrollTop
            };
        }
        /// chua su dung
    </script>
    <script type="text/javascript">
        function evaluateXPath(aNode, aExpr) {
            //            var xpe = new XPathEvaluator();
            //            var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
            //                aNode.documentElement : aNode.ownerDocument.documentElement);
            //            var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
            //            var found = [];
            //            var res;
            //            while (res = result.iterateNext())
            //                found.push(res);
            //            return found;
        }
        function searchNode(node){
            /*
        if(node==null){
            return;
        }
        try
        {
            if(node.tagName=="productName"){
                var tmp = node.firstChild.nodeValue;
                if(tmp.indexOf(strSearch, 0)>-1){
                    new_XMLDOM+="<product>";
                    var parent = node.parentNode;
                    new_XMLDOM+="<productID>"+ parent.firstChild.nodeValue+"</productID>";
                    var siblings= node.nextSibling;
                    new_XMLDOM+="<brandID>"+siblings.firstChild.nodeValue+"</brandID>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<productName>"+siblings.firstChild.nodeValue+"</productName>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<productPrice>"+siblings.firstChild.nodeValue+"</productPrice>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<productImage>"+siblings.firstChild.nodeValue+"</productImage>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<shortDecs>"+siblings.firstChild.nodeValue+"</shortDecs>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<longDecs>"+siblings.firstChild.nodeValue+"</longDecs>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<monitor>"+siblings.firstChild.nodeValue+"</monitor>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<cpu>"+siblings.firstChild.nodeValue+"</cpu>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<ram>"+siblings.firstChild.nodeValue+"</ram>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<wifi>"+siblings.firstChild.nodeValue+"</wifi>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<os>"+siblings.firstChild.nodeValue+"</os>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<pin>"+siblings.firstChild.nodeValue+"</pin>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<others>"+siblings.firstChild.nodeValue+"</others>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<sold>"+siblings.firstChild.nodeValue+"</sold>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<createDate>"+siblings.firstChild.nodeValue+"</createDate>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="<status>"+siblings.firstChild.nodeValue+"</status>";
                    siblings= node.nextSibling;
                    new_XMLDOM+="</product>";
                }
            }
            var childs = node.childNodes;
            for(var i=0;i<childs.length;i++){
                searchNode(childs[i],strSearch);
            }
        }
        catch(e) {alert(e.message)}
             */
        }
        function Search()
        {
            //            var filename="xsl/Dung.xml";
            //
            //            try
            //            {
            //                count=0;
            //                new_XMLDOM=null;
            //
            //                getHttpRequest();
            //                //  validate();
            //                if(xmlDom!=null){
            //                    strSearch =document.getElementById("txtSearchName").value;
            //
            //                    xmlDom.open("GET", filename, false)                    ;
            //                    xmlDom.send();
            //                    new_XMLDOM= xmlDom.responseXML;
            //                    count= new_XMLDOM.getElementsByTagName("product");
            //
            //                    //alert("number of products: "+count.length);
            //
            //                    var i;
            //                    for( i =0;i<count.length;i++){
            //
            //                        var tmp = count[i].getElementsByTagName("productName")[0].childNodes[0].nodeValue;
            //                        if(tmp.indexOf(strSearch)!=-1){
            //                            new_XMLDOM+="<product>";
            //                            //var tmp1=count[i].getElementsByTagName("productID")[0].childNodes[0].nodeValue;
            //                            new_XMLDOM+="<productID>"+ count[i].getElementsByTagName("productID")[0].childNodes[0].nodeValue+"</productID>";
            //                            //var tmp2=count[i].getElementsByTagName("brandID")[0].childNodes[0].nodeValue;
            //                            new_XMLDOM+="<brandID>"+count[i].getElementsByTagName("brandID")[0].childNodes[0].nodeValue+"</brandID>";
            //                            //var tmp3=count[i].getElementsByTagName("productName")[0].childNodes[0].nodeValue;
            //                            new_XMLDOM+="<productName>"+count[i].getElementsByTagName("productName")[0].childNodes[0].nodeValue+"</productName>";
            //                            //var tmp4=count[i].getElementsByTagName("productPrice")[0].childNodes[0].nodeValue;
            //                            new_XMLDOM+="<productPrice>"+count[i].getElementsByTagName("productPrice")[0].childNodes[0].nodeValue+"</productPrice>";
            //                            new_XMLDOM+="<productImage>"+count[i].getElementsByTagName("productImage")[0].childNodes[0].nodeValue+"</productImage>";
            //                            new_XMLDOM+="<shortDecs>"+count[i].getElementsByTagName("shortDecs")[0].childNodes[0].nodeValue+"</shortDecs>";
            //                            new_XMLDOM+="<longDecs>"+count[i].getElementsByTagName("longDecs")[0].childNodes[0].nodeValue+"</longDecs>";
            //                            new_XMLDOM+="<monitor>"+count[i].getElementsByTagName("monitor")[0].childNodes[0].nodeValue+"</monitor>";
            //                            new_XMLDOM+="<cpu>"+count[i].getElementsByTagName("cpu")[0].childNodes[0].nodeValue+"</cpu>";
            //                            new_XMLDOM+="<ram>"+count[i].getElementsByTagName("ram")[0].childNodes[0].nodeValue+"</ram>";
            //                            new_XMLDOM+="<wifi>"+count[i].getElementsByTagName("wifi")[0].childNodes[0].nodeValue+"</wifi>";
            //                            new_XMLDOM+="<os>"+count[i].getElementsByTagName("os")[0].childNodes[0].nodeValue+"</os>";
            //                            new_XMLDOM+="<pin>"+count[i].getElementsByTagName("pin")[0].childNodes[0].nodeValue+"</pin>";
            //                            new_XMLDOM+="<others>"+count[i].getElementsByTagName("others")[0].childNodes[0].nodeValue+"</others>";
            //                            new_XMLDOM+="<sold>"+count[i].getElementsByTagName("sold")[0].childNodes[0].nodeValue+"</sold>";
            //                            new_XMLDOM+="<createDate>"+count[i].getElementsByTagName("createDate")[0].childNodes[0].nodeValue+"</createDate>";
            //                            new_XMLDOM+="<status>"+count[i].getElementsByTagName("status")[0].childNodes[0].nodeValue+"</status>";
            //                            new_XMLDOM+="</product>";
            //                        }
            //                    }
            //                    // alert(new_XMLDOM);
            //                    var parser = new DOMParser();
            //                    new_XMLDOM = parser.parseFromString(new_XMLDOM, "text/xml");
            //
            //                    var xsl =document.implementation.createDocument("","",null);
            //                    xsl.async=false;
            //                    xsl.load('xsl/products.xsl');
            //                    combineXML_XSL(new_XMLDOM,xsl);
            //                }
            //            }
            //            catch(e) {alert(e.message)}
             
        }
        
    </script>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Products</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/xmlproject.js"></script>
    </head>
    <body id="example" onload="loadCart();">
        <div id="wrapper">
            <jsp:include page="indexHeader.jsp"></jsp:include>
            <div id="body">
                <div id="search-header" style="">
                    <div id="child-search" style="width: 100%; margin-top: -1px">
                        <div class="clear" ></div>
                        <div style="float:left; width: 87%">
                            <label style="margin-left:1%"> Product name:   </label>
                            <input type="text" name="txtSearchName" id="txtSearchName"  onkeypress="return handleEnterName(event);" maxlength="1000" style="width: 35%"/>
                            <label style="margin-left: 2%">Price from:</label>
                            <input type="text" name="priceFrom" id="priceFrom" onkeypress="return handleEnterNumber(event);" maxlength="5" style="width: 9%"/>
                            <label style="margin-left: 2%;">to:</label>
                            <input type="text" name="priceTo" id="priceTo" onkeypress="return handleEnterNumber(event);"maxlength="5" style="width: 9%"/>
                        </div>
                        <div id="searchIcon" ><img  src="images/search-button2.png " height="30px" width="30px"  name="btnSearch" onclick="SearchProduct();"/></div>
                        <div class="clear"></div>                        
                    </div>                    
                </div>
                <div class="clear"></div>
                <div id="item-show">
                    <div id="all-products">
                        <p class="title-event" style="color:#000000">						
                        </p>
                        <c:import url="xml/allProducts.xml" charEncoding="UTF-8" var="xmldoc"/>
                        <c:import url="xsl/allProducts.xsl" charEncoding="UTF-8" var="xsldoc"/>
                        <x:transform xml="${xmldoc}" xslt="${xsldoc}"/>
                    </div>
                </div><!--End item-show-->
            </div><!--End body-->
            <div id="fotter">
                <center>
                    <span> ﻿© 2023 by FPT_University. All rights reserved.</span></center>
            </div><!--End fotter-->
        </div><!--End wrapper-->
    </body>
</html>

